//----------------------------------Include------------------------------------
#include "mcu.h"
#include "glbvar.h"
//-----------------------------------------------------------------------------

//TIMER_A   total 4
//TIMER_32  total 2

//-----------------------------------Macro-------------------------------------
#define _TIMER_UNIT              (TIMER_A1_BASE)
//-----------------------------------------------------------------------------
#define USE_0                    //define USE_0 or USE_N
//-----------------------------------------------------------------------------

//--------------------------------Static Var-----------------------------------
static uint32_t    src_clk;
static uint32_t    clk_div;
static uint32_t    req_freqHz;
static uint32_t    period_cnt;
//-----------------------------------------------------------------------------

//--------------------------------Static Func----------------------------------
//-----------------------------------------------------------------------------

//--------------------------------Public Func----------------------------------
void mcu_init_tima_1(void)
{
//timer base
  src_clk    = mcu_get_smclk();
  clk_div    = 1;
  req_freqHz = 1600 *2;
  period_cnt = (src_clk/clk_div)/req_freqHz;

  Timer_A_PWMConfig  param = {0};
  param.clockSource        = TIMER_A_CLOCKSOURCE_SMCLK;
  param.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1;    //same as clk_div
  param.timerPeriod        = period_cnt;
  param.compareRegister    = TIMER_A_CAPTURECOMPARE_REGISTER_1;   //TA1C1
  param.compareOutputMode  = TIMER_A_OUTPUTMODE_SET_RESET;
  param.dutyCycle          = period_cnt/2;
  
  Timer_A_generatePWM(_TIMER_UNIT, &param);
  
//interrupt
  
//enable
}
//-----------------------------------------------------------------------------

//------------------------------------ISR--------------------------------------
//timer_a 有两个中断向量
//TA0_0_IRQHandler 对应 TAx_CCR0_CCIFG
//TA0_N_IRQHandler 对应其他 TAx_CCRx(1~6)_CCIFG 和 TAIFG ，中断原因在 TAxIV 寄存器里面查找

#ifdef USE_x
void TA1_0_IRQHandler(void)
{
  //if(Timer_A_getCaptureCompareInterruptStatus(_TIMER_UNIT, TIMER_A_CAPTURECOMPARE_REGISTER_0, TIMER_A_CAPTURECOMPARE_INTERRUPT_FLAG) == TIMER_A_CAPTURECOMPARE_INTERRUPT_FLAG){
  //  Timer_A_clearCaptureCompareInterrupt(_TIMER_UNIT, TIMER_A_CAPTURECOMPARE_REGISTER_0);
  //if(TIMER_A_CMSIS(_TIMER_UNIT)->CCTL[0] &  TIMER_A_CAPTURECOMPARE_INTERRUPT_FLAG){
  //  TIMER_A_CMSIS(_TIMER_UNIT)->CCTL[0] &= ~TIMER_A_CAPTURECOMPARE_INTERRUPT_FLAG;   //必须清除中断
  
  if(TA0CCTL0 &  CCIFG){
    TA0CCTL0 &= ~CCIFG;   //必须清除中断
    
    //igu64_tick_ms++;
    
    static uint32_t   cnt;
    if(++cnt > 1000){
      cnt = 0;
      //mcu_led_toggle();
    }
  }
}
#endif

#ifdef USE_N
void TA1_N_IRQHandler(void)
{
  if(Timer_A_getInterruptStatus(_TIMER_UNIT) == TIMER_A_INTERRUPT_PENDING){   //对应 TAIFG
    Timer_A_clearTimerInterrupt(_TIMER_UNIT);
    
    igu64_tick_ms++;

    static uint32_t   cnt;
    if(++cnt > 1000){
      cnt = 0;
      mcu_led_toggle();
    }
  }
}
#endif
//------------------------------------EOF--------------------------------------





